package com.fy.fyai.common.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;

import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * @author fwq
 * @description AI聊天配置
 * @date 2025-10-30 18:22
 */
@Configuration
public class AiChatConfig {

    @Value("${spring.ai.chatmemory.top-k:}")
    private int topK;

    @Bean
    public ChatClient chatClient(OllamaChatModel model, ChatMemoryService chatMemoryService) {
        return ChatClient
                .builder(model)
                .defaultSystem("你是一个热心、可爱的智能助手，你的名字叫小团团，请以小团团的身份和语气回答问题。")
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemoryService), new SimpleLoggerAdvisor())
                .defaultAdvisors(chatMemoryRetrieve
                        -> chatMemoryRetrieve.param(CHAT_MEMORY_RETRIEVE_SIZE_KEY,
                        topK = topK > 0 ? topK : 5))
                .build();
    }
}
